﻿using ConfExpert.Forms.Base;
using DevExpress.Utils;
using DevExpress.XtraEditors;
using DevExpress.XtraGrid;
using DevExpress.XtraGrid.Views.Grid;
using DevTeam;
using System;
using System.Drawing;
using System.Windows.Forms;

namespace ConfExpert.Forms.SatinAlma
{
    public partial class YearlyPerf : XForm
    {
        public YearlyPerf()
        {
            InitializeComponent();
        }

        BindingSource source = new BindingSource();
        private void YearlySales_Load(object sender, EventArgs e)
        {
            gridView1.Init("GetVendorPerf2");
            source = gridView1.AddDetailGrid(gridView2, "VendorId");

            AddDataBar(gridView1, "Qty2014");
            AddDataBar(gridView1, "Qty2015");
            AddDataBar(gridView1, "Qty2016");

            gridView2.MakeColumnInvisible("Yil");
            gridView1.Columns["Qty2014"].ToolTip = "*7. aydan sonrası";
            gridView1.FormatAsNumber(1, "Qty2014", "Qty2015", "Qty2016");
            gridView1.AddSummary("Qty2014", "Qty2015", "Qty2016");
            gridView2.FormatAsNumber(1, "TotalQty", "Numune", "Uretim", "Koleksiyon", "MusteriOdemeli");
            gridView2.AddSummary("TotalQty", "Numune", "Uretim", "Koleksiyon", "MusteriOdemeli");

            gridView1.RowCellStyle += gridView1_RowCellStyle;
            gridView2.RowCellStyle += gridView1_RowCellStyle;

            gridView1.FocusedColumnChanged += gridView1_FocusedColumnChanged;
            gridView1_FocusedColumnChanged(null, null);

            gridView1.BestFitColumns();
            gridView2.BestFitColumns();
        }

        void gridView1_RowCellStyle(object sender, DevExpress.XtraGrid.Views.Grid.RowCellStyleEventArgs e)
        {
            switch (e.Column.FieldName)
            {
                case "VendorId":
                case "TotalQty":
                    e.Appearance.Font = new Font(AppearanceObject.DefaultFont, FontStyle.Bold);
                    break;
            }
        }

        private void AddDataBar(GridView gridView, string col)
        {
            GridFormatRule gridFormatRuleY = new GridFormatRule();
            FormatConditionRuleDataBar formatConditionRuleDataBarY = new FormatConditionRuleDataBar();
            gridFormatRuleY.Column = gridView.Columns[col];
            formatConditionRuleDataBarY.PredefinedName = "Green Gradient";
            gridFormatRuleY.Rule = formatConditionRuleDataBarY;
            gridView.FormatRules.Add(gridFormatRuleY);
        }

        void gridView1_FocusedColumnChanged(object sender, DevExpress.XtraGrid.Views.Base.FocusedColumnChangedEventArgs e)
        {
            source.Filter = "Yil=" + gridView1.FocusedColumn.FieldName.Replace("Qty", string.Empty).ToInt();
            gridView2.BestFitColumns();
        }

    }
}
